package cn.lesper.process;

import cn.lesper.model.ColumnEntity;
import cn.lesper.model.TableEntity;
import cn.lesper.utils.DatabaseUtils;

import java.util.ArrayList;
import java.util.List;

//逻辑处理层
public class AbstractProcess extends AbstractExtensible{

    DatabaseUtils databaseUtils = new DatabaseUtils();
    StringBuilder stringBuilderTemp = new StringBuilder();
    StringBuilder stringBuilder = new StringBuilder();
    //可缓存,后续加上是否热更新开关
    TableEntity tableEntity = new TableEntity();
    List<ColumnEntity> columnEntityList = new ArrayList<>();
    private String path = this.getClass().getClassLoader().getResource("").getPath();

    //用于测试的xml
    String testPath = "C:\\Users\\surge\\Desktop\\java\\myworks\\quattro\\native-jdbc\\src\\main\\resources\\test.xml";

    //初始化表单结构,为后面操作奠定基础
    private void init(String tableName) throws Exception {
        //表填充
        if (!tableName.equals(tableEntity.getTableName())) {
            tableEntity = addTableEntity();
            columnEntityList = addTableEntity().getCloumn();
        }
    }



    //===========================================sql process===============================================

    protected String insertSql(){
        stringBuilderTemp = new StringBuilder();
        stringBuilder = new StringBuilder();
        String tableName = tableEntity.getTableName();
        stringBuilderTemp.append("INSERT INTO " + tableName + "(");
        for (ColumnEntity columnEntity : columnEntityList) {
            String cloumnName = columnEntity.getCloumnName();
            stringBuilderTemp.append(cloumnName);
            stringBuilderTemp.append(",");
        }

        stringBuilder.append(stringBuilderTemp.substring(0, stringBuilderTemp.length() - 1));
        stringBuilder.append(") VALUES");

        for (ColumnEntity columnEntity : columnEntityList) {
            String cloumnName = columnEntity.getCloumnName();
            stringBuilderTemp.append(":"+cloumnName);
        }

        return stringBuilder.toString();
    }

    protected String deleteSql(List<String> condition){
        stringBuilderTemp = new StringBuilder();
        stringBuilder = new StringBuilder();
        String tableName = tableEntity.getTableName();
        stringBuilderTemp.append("DELETE FROM " + tableName);
        for (ColumnEntity columnEntity : columnEntityList) {
            String cloumnName = columnEntity.getCloumnName();
            stringBuilderTemp.append(cloumnName);
            stringBuilderTemp.append(",");
        }

        stringBuilder.append("WHERE ");
        for (String str : condition) {
            stringBuilder.append(str+ "=:" +str);
        }

        return stringBuilder.toString();
    }

    protected String updateSql(List<String> condition){
        stringBuilderTemp = new StringBuilder();
        stringBuilder = new StringBuilder();
        String tableName = tableEntity.getTableName();
        stringBuilderTemp.append("UPDATE " + tableName);
        for (ColumnEntity columnEntity : columnEntityList) {
            String cloumnName = columnEntity.getCloumnName();
            stringBuilderTemp.append(cloumnName + "=:" + cloumnName);
            stringBuilderTemp.append(",");
        }

        stringBuilder.append(stringBuilderTemp.substring(0, stringBuilderTemp.length() - 1));
        stringBuilder.append("WHERE ");
        for (String str : condition) {
            stringBuilder.append(str+ "=:" +str);
        }

        return stringBuilder.toString();
    }

    protected String querySql() {
        stringBuilderTemp = new StringBuilder();
        stringBuilder = new StringBuilder();
        String tableName = tableEntity.getTableName();
        stringBuilderTemp.append("SELECT ");
        for (ColumnEntity columnEntity : columnEntityList) {
            String cloumnName = columnEntity.getCloumnName();
            stringBuilderTemp.append(cloumnName);
            stringBuilderTemp.append(",");
        }
        stringBuilder.append(stringBuilderTemp.substring(0, stringBuilderTemp.length() - 1));
        stringBuilder.append("FROM " + tableName);
        return stringBuilder.toString();
    }

}
